from django.db import models
from db.abstract_model import AbstractModel
from tinymce.models import HTMLField
from utils.wrapper import *


class StoryTypeManager(models.Manager):
    def get_name_by_id(self,param):
        try:
            return self.get(id=param).type
        except:
            pass


class StoryType(AbstractModel):
    type = models.CharField(max_length=20, verbose_name="文章类型")
    objects = StoryTypeManager()

    def __str__(self):
        return self.type

    class Meta:
        verbose_name_plural = "文章类型"
        verbose_name = "文章类型"


class StoryManager(models.Manager):
    # 根据文章类型获取文章
    def get_story_by_type(self,type):
        try:
            return self.filter(type_id=type).order_by("-update_time")
        except:
            pass

    def get_story_by_id(self,id):
        try:
            return self.get(id=id)
        except:
            pass

class Story(AbstractModel):
    type = models.ForeignKey(StoryType,verbose_name="文章类型")
    title = models.CharField(max_length=100, verbose_name="文章标题")
    short = models.CharField(max_length=200, verbose_name="文章简述")
    title_img = models.ImageField(upload_to="images/story/",verbose_name="标题图片")
    desc = HTMLField(verbose_name="故事内容")

    objects = StoryManager()

    class Meta:
        verbose_name_plural = "文章"
        verbose_name = "文章"